jQuery(function($){
	var prodStock={
			grid : null,
			init:function(){
				if (roles.length > 0) {
					var add = [];
					var download = [];
					var exportExcel = [];
					var items = [];			
					for(var i=0; i<roles.length; i++) {
			    		if (roles[i] == 'SYSTEM_ADMIN' || roles[i] == 'WAREHOUSE_SUPERVISOR') {
			    			add = [{ text: "库存盘点",
			    				     id:"add", 
			    				     click: function(){			    						                 
			    				    	prodStock.uploadFile();			    				
			    			         }, 
			    			         icon: "add"},{ line: true }];
			    			adde = [{ text: "盘点记录",
		    				     id:"remove", 
		    				     click: function(){			    						                 
		    				    	prodStock.addInventory();			    				
		    			         }, 
		    			         icon: "search"},{ line: true }];
			    			download = [{ text: '<a id="downloadTemplate" href="'+basePath+'/product/productmgt/downloadTemplet.do">下载库存模板</a>',
		    				     id:"download", 
		    				     click: function(){			    						                 
		    			         },icon: "down"},{ line: true }];
							exportExcel = [{text: "导出EXCEL",
		   				     	id:"export", 
		   				     	click: function(){
		   				     		prodStock.exportExcel();
		   				     	}, icon: "up"},{ line: true }];
			    		}
					}
					items = items.concat(add);
					items = items.concat(adde);
					items = items.concat(download);
					items = items.concat(exportExcel);
					toolbar = {items : items};
				}
				$("#btnSearch").on("click",function(){
					prodStock.grid.set({
						parms: [
								{name: "commoName", value:$("#commoName").val()},
								{name: "commoNo", value:$("#commoNo").val()},
								{name: "prodNo", value:$("#prodNo").val()},
								{name: "supNo", value:$("#supName").val()},
								{name: "whNo", value:$("#whNo").val()}
			                    ],
						url: "productStockPageList.do"						
					});
					//prodStock.grid.loadData();
					
				});
				var supplierConfig={
						url:"/base/supplierInfo/supplierList.do",
						dom:$("#supName"),
						value:"code",
						text:"supName"
				};
				this.setSelectBox(supplierConfig);
				var whareHouseConfig={
						url:"/base/wharehouse/whareHouseList.do",
						dom:$("#whNo"),
						value:"whNo",
						text:"whName"
				};
				this.setSelectBox(whareHouseConfig);
				this.pageList();
			},
			pageList:function(){
				prodStock.grid = $("#maingrid").ligerGrid({
		            columns: [
		            { display: '仓库名称', name: 'whName'},
		            { display: '供应商名称', name: 'supName'},
		            { display: '货品编号', name: 'prodNo'},
		            { display: '商品编号', name: 'commoNo'},
		            { display: '商品名称', name: 'commoName'},
		            { display: '分类名称', name: 'catName'},
		            { display: '品牌名称', name: 'brandName'},
		            { display: '商品规格', name: 'specName'},
		            { display: '良品数量', name: 'qualifiedQuantity'},
		            { display: 'vip预占数量', name: 'vipOccupyQuantity',width:80},
		            { display: '包裹预占数量', name: 'preOccupyQuantity',width:90,render: function (rowdata, rowindex, value){
		            	return ((rowdata.preOccupyQuantity?rowdata.preOccupyQuantity:0)-(rowdata.vipOccupyQuantity?rowdata.vipOccupyQuantity:0))+"";
		            }},
		            { display: '可用数量', name: 'preOccupyQuantity',render: function (rowdata, rowindex, value){
		            	return (rowdata.qualifiedQuantity-(value?value:0))+"";
		            }},
		            { display: '次品数量', name: 'defectiveQuantity'},
		            { display: '商品总数', name: 'productTotal'},
		            { display: '更新时间', name: 'updateTime', width:'10%'},
		            { display: '操作',width:'15%', isSort: false, render: function (rowdata, rowindex, value){
		            	var text =  "<a href='javascript:showCommodityInfo(" + rowindex + ",1)'>商品信息</a> ";
		            	text +=  "<a href='javascript:openSetVipOccupyQuantityWindow(" + rowindex + ")'>设置vip预占数</a> ";
		            	return text;
		            }}], 
		            pageSize:30,
		            width: '100%',
		            height: '100%',
		            dataType: 'server', 
		            dataAction: 'server',
		            pagesizeParmName: 'pageSize',
		            parms: [
							{name: "commoName", value:$("#commoName").val()},
							{name: "commoNo", value:$("#commoNo").val()},
							{name: "prodNo", value:$("#prodNo").val()},
							{name: "supNo", value:$("#supName").val()},
							{name: "whNo", value:$("#whNo").val()}
		                    ],
					url: "productStockPageList.do",
					toolbar: toolbar
		        });
				grid=prodStock.grid;
			},
			setSelectBox:function(config){
				$.ajax({
					url:basePath+config.url,
					type:"post",
					dataType:"json",
					success:function(result){
						if(result  && result.length){
							config.dom.html('<option value="">请选择</option>');
							for(var x in result){
								config.dom.append('<option value="'+result[x][config.value]+'">'+result[x][config.text]+'</option>');
							}
						}
					}
				});
			},
			exportExcel:function(){
				var exportUrl = basePath+"/product/productmgt/exportProductStockList.do";
				var params=[{name: "commoName", value:$("#commoName").val()},
    						{name: "commoNo", value:$("#commoNo").val()},
    						{name: "prodNo", value:$("#prodNo").val()},
    						{name: "supNo", value:$("#supName").val()},
    						{name: "whNo", value:$("#whNo").val()}
    	                    ];
			            var form = $("<form>");   //定义一个form表单
			            form.attr('style', 'display:none');   //在form表单中添加查询参数
			            form.attr('target', '');
			            form.attr('method', 'post');
			            form.attr('action', exportUrl);
			            for(var i=0;i<params.length;i++){
			            	var input = $('<input>');
				            input.attr('type', 'hidden');
				            input.attr('name', params[i].name);
				            input.attr('value', params[i].value);
				            form.append(input);   //将查询参数控件提交到表单上
			            }
			            $('body').append(form);  //将表单放置在web中 
			            form.submit();
			            form.remove();
			},
			addInventory:function(){
				var title = "库存盘点记录";
		    	window.parent.f_addTab(title, title, '../../../inventory/inventoryManger/inventoryCheckList.do');
			},
			uploadFile:function(){
		      	$("#importDiv").html('<input type="file" id="fileupload" name="file">');
		      	$("#fileupload").click();
		    	var ajaxUploadUrl = basePath + "/product/productmgt/importStock.do";
		      	$("#fileupload").change(function(){
	    	   	  	var filename = this.value;
	    	   	  	var extend = filename.substring(filename.lastIndexOf(".")+1).toLowerCase();
	    	      	if (extend != "xls" && extend !=  "xlsx"){
	    				window.parent.f_tip("请选择xls和xlsx文件导入");
	    			}else{
	    	       	  $.ligerDialog.waitting("正在导入中,请不要关闭当前页面..."); 
	    	       	  var options = {
	    	       		url : ajaxUploadUrl,
	    	       		fileElementId: "fileupload",
	    	       		secureuri: false,
	    	       		dataType: 'text',
	    				success: function(result){
	    					if(result.indexOf('error') > -1){
	    						window.parent.f_tip('库存盘点失败');
	    					}else {
	    						window.parent.f_tip('库存盘点成功');
	    						$.ligerDialog.closeWaitting();  
	    						var url=basePath + "/product/productmgt/toCheckProductStock.do?pTabid="+WIN_TAB.getSelectedTabItemID();
	    					   	window.parent.f_addTab('商品库存盘点', '商品库存盘点',url );	
	    					}
	    					$.ligerDialog.closeWaitting();  
	    				}
	    	       	  }; 
	    	       	  	$.ajaxFileUpload(options);
	    	         }		      		
		      	});
			},
			downloadTemplate:function(){
				$("#downloadTemplate").click();
			}
	};
	prodStock.init();
}); 